package kyz.icom.healthfrags;

/**
 * This is a secondary activity, to show what the user has selected
 * when the screen is not large enough to show it all in one activity.
 */

//import kyz.icom.healthfrags.MainApplicationEntryPoint.DetailsFragment;
import working_fragment_tabs.AllergyEditFragmentTab2;
import working_fragment_tabs.AllergySummaryFragmentTab3;
import working_fragment_tabs.AllergyViewFragmentTab1;
import android.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class DetailsActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// If the screen is now in landscape mode, we can show the
			// dialog in-line with the list so we don't need this activity.
			finish();
			return;
		}

		if (savedInstanceState == null) {
			// During initial setup, plug in the details fragment.
			// DetailsFragment details = new DetailsFragment();
			// details.setArguments(getIntent().getExtras());
			// getFragmentManager().beginTransaction().add(android.R.id.content,
			// details).commit();

			ActionBar actionBar = getActionBar();
			actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			String label1 = getResources().getString(R.string.label1);
			Tab tab = actionBar.newTab();
			tab.setText(label1);
			TabListener<AllergyViewFragmentTab1> tl = new TabListener<AllergyViewFragmentTab1>(
					this, label1, AllergyViewFragmentTab1.class);
			tab.setTabListener(tl);
			actionBar.addTab(tab);

			String label2 = getResources().getString(R.string.label2);

			tab = actionBar.newTab();

			tab.setText(label2);

			TabListener<AllergyEditFragmentTab2> tl2 = new TabListener<AllergyEditFragmentTab2>(
					this,

					label2, AllergyEditFragmentTab2.class);

			tab.setTabListener(tl2);

			actionBar.addTab(tab);

			String label3 = getResources().getString(R.string.label3);
			tab.setText(label3);

			TabListener<AllergySummaryFragmentTab3> tl3 = new TabListener<AllergySummaryFragmentTab3>(
					this,

					label2, AllergySummaryFragmentTab3.class);

			tab.setTabListener(tl3);

			actionBar.addTab(tab);

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.actions, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_home:
			Intent intent = new Intent(this, MainApplicationEntryPoint.class);
			startActivity(intent);
			break;

		}
		return super.onOptionsItemSelected(item);
	}

	private class TabListener<T extends Fragment> implements
			ActionBar.TabListener {
		private Fragment mFragment;
		private final FragmentActivity mActivity;
		private final String mTag;
		private final Class<T> mClass;

		public TabListener(FragmentActivity activity, String tag, Class<T> clz) {
			mActivity = activity;
			mTag = tag;
			mClass = clz;

		}

		@SuppressWarnings("unused")
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			// Check if the fragment is already initialized
			if (mFragment == null) {
				// If not, instantiate and add it to the activity
				mFragment = Fragment.instantiate(mActivity, mClass.getName());
				ft.add(android.R.id.content, mFragment, mTag);
			} else {
				// If it exists, simply attach it in order to show it
				ft.attach(mFragment);
			}

		}

		@SuppressWarnings("unused")
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			if (mFragment != null) {
				// Detach the fragment, because another one is being attached
				ft.detach(mFragment);
			}

		}

		@SuppressWarnings("unused")
		public void onTabReselected(Tab tab, FragmentTransaction ft) {

			// User selected the already selected tab. Usually do nothing.

		}

		@Override
		public void onTabReselected(Tab arg0,
				android.app.FragmentTransaction arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTabUnselected(Tab arg0,
				android.app.FragmentTransaction arg1) {
			// TODO Auto-generated method stub

		}

	}

}